﻿using FireChat.Server.Database.Manager;
using FireChat.Server.Session.Receiver;
using FireChat.Share.Web.Actions;
using FireChat.Share.Web.Model.Query.Account;
using FireChat.Share.Web.Package;

namespace FireChat.Server.Session.Actions.Account;


/// <summary>
/// 登录账号
/// </summary>
internal class LoginAction : ServerAction<LoginQuery>
{
    public override ServertActions Type => ServertActions.Login;

    public override bool IsVerifyAccountToken => false;

    protected override ServerPackage ActionExcute(PackageReceiver receiver, LoginQuery query)
    {
        //如果存在登录信息
        if (receiver.Session.VerifyAccountToken())
        {
            var db_account = AccountManager.GetInstanceByName(query.Username);

            if (db_account != null)
            {
                if (receiver.Session.AccountToken.UID == db_account.Id)
                {
                    return receiver.ReplyFailureAsync("账户已登录 请勿重复登录");
                }
                return receiver.ReplyFailureAsync("请先退出当前登录");
            }
        }

        return receiver.Login(query);
    }
}
